package com.yu.work.thread;

import android.os.Handler;
import android.os.Message;

import java.util.Random;

/**
 * 随机抽号线程
 */
public class DrawNumberThread extends Thread {

    private static Random random = new Random();

    private boolean flag = true;
    private int maxNumber = -1;
    private Handler handler;

    public DrawNumberThread(Handler handler) {
        this.handler = handler;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public void setMaxNumber(int maxNumber) {
        if (maxNumber > 0) {
            this.maxNumber = maxNumber;
        } else {
            this.maxNumber = -1;
        }
    }

    @Override
    public void run() {
        while (flag && maxNumber != -1) {
            //随机生成目标号码
            int target = random.nextInt(maxNumber) + 1;
            Message msg = new Message();
            msg.what = 1;
            msg.obj = target;
            handler.sendMessage(msg);

            try {
                Thread.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
